package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.Chapter;
import com.itany.netclass.entity.Course;
import com.itany.netclass.entity.SearchForm;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.ChapterService;
import com.itany.netclass.util.ParameterUtil;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-16 下午2:49
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/chapter")
public class ChapterController {
    @RequestMapping("/findAllChapterByCourseId")
    public  String findAllChapter(HttpServletRequest request, HttpServletResponse response){

        request.getSession().setAttribute("NULL", null);
        request.getSession().setAttribute("chapterSearchForm",new SearchForm());

        //开始查询
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        String courseIdG = request.getParameter("courseId");
        String courseIdS = (String) request.getSession().getAttribute("courseId");//courseIdS为当前页的courseId，
        System.out.println(courseIdG);
        System.out.println(courseIdS);
        if(!ParameterUtil.isNull(courseIdG)&&!courseIdG.equals(courseIdS)){//两者不相同表示，重新进入页面且换了一个course，所有需要更换，
            courseIdS=courseIdG;
            request.getSession().setAttribute("courseId",courseIdS);
        }
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(ParameterUtil.isNull(pageNo)){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Chapter> list= chapterService.findAllChapterByCourseId(courseIdS);
            PageInfo<Chapter> info =new PageInfo<>(list);
            request.getSession().setAttribute("chapterList",info);
            return "redirect:/showChapter.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            request.getSession().setAttribute("courseId",null);
            return "/course/findAllType.do";
        } catch (NullSringException e) {
            e.printStackTrace();
            request.getSession().setAttribute("chapterList",new PageInfo<>());
            request.getSession().setAttribute("NULL", e.getMessage());
            return "redirect:/showChapter.do";

        }
    }
    @RequestMapping("/findChapterByForm")
    public String findChapterByForm(HttpServletRequest request,HttpServletResponse response){
        String chapterName = request.getParameter("chapterName");
        System.out.println("chapterName="+chapterName);
        String chapterInfo = request.getParameter("chapterInfo");
        System.out.println("chapterInfo="+chapterInfo);
        String kaishitime = request.getParameter("kaishitime");
        System.out.println("kaishitime="+kaishitime);
        String jieshutime = request.getParameter("jieshutime");
        System.out.println("jieshutime="+jieshutime);
        String status = request.getParameter("course-resource-stauts-search");
        System.out.println("status="+status);

        //取出courseId
        String courseId= (String) request.getSession().getAttribute("courseId");

        //存放条件以便分页查询
        SearchForm searchForm =new SearchForm();
        searchForm.setChanpter(chapterName);
        searchForm.setInfo(chapterInfo);
        searchForm.setStatus(status);
        searchForm.setKaishitime(kaishitime);
        searchForm.setJiesutime(jieshutime);
        request.getSession().setAttribute("chapterSearchForm",searchForm);

        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(ParameterUtil.isNull(pageNo)){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize=PageConstant.PAGE_SIZE;
        }
        List<Chapter> list= null;
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            list = chapterService.findChapterByForm(courseId,chapterName,chapterInfo,kaishitime,jieshutime,status);
            PageInfo<Chapter> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("chapterList",pageInfo);
            request.getSession().setAttribute("NULL",null);
            return "/showChapter.do";
        } catch (NullSringException e) {
            e.printStackTrace();
            request.getSession().setAttribute("chapterList",null);
            request.getSession().setAttribute("NULL", e.getMessage());
            return "/showChapter.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/chapter/findAllChapterByCourseId.do";
        }
    }
    @RequestMapping("/findChapterByForm2")//动态Sql之后的分页
    public String findChapterByForm2(HttpServletRequest request, HttpServletResponse response){
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        String courseId= (String) request.getSession().getAttribute("courseId");
        SearchForm searchForm = (SearchForm) request.getSession().getAttribute("chapterSearchForm");
        String chapterName= searchForm.getChanpter();
        String chapterInfo = searchForm.getInfo();
        String kaishitime = searchForm.getKaishitime();
        String jieshutime = searchForm.getJiesutime();
        String status = searchForm.getStatus();

        String pageNo=null;//响应后显示的页数
        String pageNo1 = request.getParameter("pageNo");//get传过来的页数

        PageInfo<Course> Info = (PageInfo<Course>) request.getSession().getAttribute("chapterList");
        String pageNo2="";
        if(Info!=null){
            pageNo2=""+Info.getPageNum();
            System.out.println(pageNo2);
        }
        System.out.println(pageNo2);
        //这个是表示在找到结果是如果数据改了，导致页面变少，自动跳转到前一页的页数
        String pageNo3 = (String) request.getSession().getAttribute("qianyiye");
        System.out.println("pageNo1="+pageNo1);
        System.out.println("pageNo2="+pageNo2);
        System.out.println("pageNo3="+pageNo3);


        //以下为使用哪一个作为当前页数的判断
        if(pageNo3!=null){
            System.out.println("用了前一页");
            pageNo=pageNo3;
        }else if(pageNo1!=null){
            System.out.println("用了get");
            pageNo=pageNo1;
        }else if(!ParameterUtil.isNull(pageNo2)){
            System.out.println("用了session");
            pageNo=pageNo2;
        }else {
            System.out.println("用了常量");
            pageNo=PageConstant.PAGE_KAISHI_NO;
        }

        String pageSize = request.getParameter("pageSize");
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<Chapter>list = chapterService.findChapterByForm(courseId,chapterName,chapterInfo,kaishitime,jieshutime,status);
            PageInfo<Chapter> pageInfo=new PageInfo<>(list);

//            查到了结果清空存放前一页和五错误对象的session，不然下次查询或页面切换可能会出错
            request.getSession().setAttribute("qianyiye",null);
            request.getSession().setAttribute("NULL",null);
            request.getSession().setAttribute("chapterList",pageInfo);
            return "/showChapter.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/course/findAllChapterByCourseId.do";
        } catch (NullSringException e) {//表示当前页数无任何数据的异常需要跳转
            if(pageNo.equals(PageConstant.PAGE_KAISHI_NO)){
                //已经是第一页，表示为无满足条件的对象
                request.getSession().setAttribute("chapterList",new PageInfo<>());
                request.getSession().setAttribute("NULL","未查询到任何结果");
                return "/showChapter.do";
            }else {
                //设置前一页的页码放到session中
                String pageNum = ""+(Info.getPageNum()-1);
                request.getSession().setAttribute("qianyiye",pageNum);
                return "/chapter/findChapterByForm2.do";
            }
        }
    }
    @ResponseBody
    @RequestMapping("/yongChapterById")//修改状态，启用和禁用
    public ResponseResult yongChapterById(HttpServletRequest request, HttpServletResponse response){
      ResponseResult result=new ResponseResult();
        String courseId= (String) request.getSession().getAttribute("courseId");
        String chapterId = request.getParameter("chapterId");
        System.out.println("chapterId="+chapterId);
        String status = request.getParameter("status");
        System.out.println("status="+status);
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.yongChapterById(courseId,chapterId,status);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaTitle")
    public  ResponseResult jianchaTitle(HttpServletRequest request,HttpServletResponse response){
      ResponseResult result=new ResponseResult();
      ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterService");
        String title = request.getParameter("title");
        String courseId = (String) request.getSession().getAttribute("courseId");
        String chapterId = request.getParameter("chapterId");
        try {
            chapterService.jianchaTitle(chapterId,courseId,title);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (CharpetExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaInfo")
    public  ResponseResult jianchaInfo(HttpServletRequest request,HttpServletResponse response){
      ResponseResult result=new ResponseResult();
        String info = request.getParameter("info");
        System.out.println("info="+info);
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.jianchaInfo(info);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaZhiyuantitle")
    public  ResponseResult jianchaZhiyuantitle(HttpServletRequest request,HttpServletResponse response){
      ResponseResult result=new ResponseResult();
        String zhiyuantitle = request.getParameter("zhiyuantitle");
        System.out.println("zhiyuantitle="+zhiyuantitle);
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.jianchaZhiyuantitle(zhiyuantitle);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ResourceExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaCostNum")
    public  ResponseResult jianchaCostNum(HttpServletRequest request,HttpServletResponse response){
      ResponseResult result=new ResponseResult();
        String costNum = request.getParameter("costNum");
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.jianchaCostNum(costNum);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/jianchaResource")
    public  ResponseResult jianchaResource(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
      ResponseResult result=new ResponseResult();
        String resourceType = request.getParameter("resource_type_id");
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.jianchaResource(list,resourceType);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (BuPiPeiException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/addChapterAndResource")
    public  ResponseResult addChapterAndResource(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
      ResponseResult result=new ResponseResult();
        String title = request.getParameter("title");
        System.out.println("title="+title);
        String info = request.getParameter("info");
        System.out.println("info="+info);
        String resourceTitle = request.getParameter("resourceTitle");
        System.out.println("resourceTitle="+resourceTitle);
        String costType = request.getParameter("file_cost_type_id");
        System.out.println("costType="+costType);
        String costNum = request.getParameter("costNum");
        System.out.println("costNum="+costNum);
        String resourceType = request.getParameter("resource_type_id");
        System.out.println("resourceType="+resourceType);
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.addChapterAndResource(request.getSession(),title,info,resourceTitle,costType,costNum,resourceType,list);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/findChapterAndResourceBychapterId")
    public  ResponseResult findChapterAndResourceBychapterId(HttpServletRequest request,HttpServletResponse response){
      ResponseResult result=new ResponseResult();
        String chapterId = request.getParameter("chapterId");
        System.out.println("chapterId="+chapterId);
        ChapterService chapterService = (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            Chapter chapter = chapterService.findChapterAndResourceByChapterId(chapterId);
            result.setReturnObject(chapter);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyChapterAndResource")
    public  ResponseResult modifyChapterAndResource(HttpServletRequest request,HttpServletResponse response,List<CommonsMultipartFile> list){
      ResponseResult result=new ResponseResult();
        String chapterId = request.getParameter("chapterId");
        System.out.println("chapterId"+chapterId);
        String title = request.getParameter("title");
        System.out.println("title="+title);
        String info = request.getParameter("info");
        System.out.println("info="+info);
        String resourceTitle = request.getParameter("resourceTitle");
        System.out.println("resourceTitle="+resourceTitle);
        String costType = request.getParameter("file_cost_type_id");
        System.out.println("costType="+costType);
        String costNum = request.getParameter("costNum");
        System.out.println("costNum="+costNum);
        String resourceType = request.getParameter("resource_type_id");
        System.out.println("resourceType="+resourceType);
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterService");
        try {
            chapterService.modifyChapterAndResource(request.getSession(),chapterId,title,info,resourceTitle,costType,costNum,resourceType,list);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
